<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
//error_reporting(E_ALL);
class Clinic extends CI_Controller {
	function __construct()
	    {
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('session');
                $this->load->model('mod_clinic');
                $this->load->library('lang');
                $this->load->library('input');
	    }

	function index()
	{
		$this->load->view('clinic/vue_profilClinic');
	}
        function addCompteClinic(){
                $this->form_validation->set_rules('email','"Adresse email"','trim|required|xss_clean|valid_email|is_unique[compte_clinique.EMAIL]');
                $this->form_validation->set_rules('password_clinic','"Mot de passe"','trim|required|xss_clean');
                if($this->form_validation->run()==FALSE){
                    $this->load->view('stdr/vue_accueil');
                } else {
                        $data=array();
			$email = $this->input->post('email');
                        $compte = array(
					'EMAIL' => $email ,
					'MDP' => $this->input->post('password_clinic'),
                                        'ETAT'=>0
                                                );
                        $this->mod_clinic->addCompteClinic($compte);
                        $id_compte = $this->mod_clinic->getIdCompteByMail($email);
                        $data['ID_COMPTE_CLI']=$id_compte;
                        $this->mod_clinic->addInfoClinic($data);
                        echo '<script>alert("INSCRIPTION REUSSIE");document.location="../";</script>';
                        }    
                        
               }
               function connexionClinic() {
                $this->form_validation->set_rules('email','"Adresse mail"','trim|required|xss_clean|valid_email');
                $this->form_validation->set_rules('password_clinic','"Mot de passe"','trim|required|xss_clean');
                if($this->form_validation->run()==FALSE){
                    $this->load->view('accueil');
                } else {
		$email = $this->input->post('email');
		$mdp = $this->input->post('password_clinic');
		$id_compte_cli = $this->mod_clinic->verifCompteClinic($email,$mdp);
                $id_cli = $this->mod_clinic->getClinicByIdCompte($id_compte_cli);
		if ($id_cli!=0){
			//echo $result;
		   $newdata = array(
			   'idemail'  => $email,
			   'logged_in' => 'clinic',
			   'idid' => $id_cli
		  );    
		   $_SESSION['idemail'] = $email;	
		   $_SESSION['logged_in'] = 'clinic';	   
		   $_SESSION['idid'] = $id_cli;   
		/*if($isInfoUpdate == 1){
			echo '<script>document.location = "updateC1affiche?id='.$result.'";</script>';
		}else{
			echo '<script>document.location = "updateC1?id='.$result.'";</script>';
		}*/
			//echo '<script>document.location = "updateC1?id='.$result.'";</script>';
                   echo '<script>document.location="../fr/espace-clinique/";</script>';
		   $this->session->set_userdata($newdata);	
		}
		else {
			//echo "ts ok";
			echo '<script>alert("Erreur login ou mot de passe"); document.location = "../index.php";</script>';
			//header("location:updateC1?id=1");
			//$this->load->view('pagedn/header');	
			//$this->load->view('pagedn/ajoutC1');	
			//$this->load->view('pagedn/footer');	
		}
           }
        
        }
        function step_laclinique(){
                $lg = $this->uri->segment(1,0);
                $_SESSION['lg']=$lg;
                $id_cli = $_SESSION['idid'];
                $this->lang->load('header',$lg);
                $this->lang->load('lien',$lg);
                $this->lang->load('container',$lg);
                $this->lang->load('footer',$lg);
                $data['result']= $this->mod_clinic->getAllClinicInfo($id_cli);
                $data['pays']= $this->mod_clinic->getAllPays();
                $data['origine_materiaux3']= $this->mod_clinic->getAllPays();
                $data['origine_materiaux2']= $this->mod_clinic->getAllPays();
                $data['origine_materiaux1']= $this->mod_clinic->getAllPays();
                $this->load->view('clinic/vue_step_la_clinique',$data);
            
        }
        function updateClinic(){
            $lg = $this->uri->segment(1,0);
            $_SESSION['lg']=$lg;
            $id_cli = $_SESSION['idid'];
            $this->lang->load('header',$lg);
            $this->lang->load('lien',$lg);
            $this->lang->load('container',$lg);
            $this->lang->load('footer',$lg);
            $pays = $this->input->post('pays');
            $ville = $this->input->post('ville');
            $adresse = $this->input->post('adresse');
            $map = $this->input->post('map');
            echo $pays."<br />".$ville."<br />".$adresse."<br />".$map;             
		$id_clinique = $_SESSION['idid'];
		//echo "mijakely".$id_clinique;
		if (isset($_POST['submit']))
		{
			$iCnt = $this->input->post('iCnt');
			$isInfoUpdate = 1;
			$marque_implant1 = "";$marque_implant2 = "";$marque_implant3 = "";
			//echo "domoina".$iCnt;
			if($iCnt==0){
				$marque_implant1=$this->input->post('marque_implant1');
			}
			if($iCnt==1){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$marque_implant1=$valueexpanne1;
			}
			else if($iCnt==2){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$marque_implant1=$valueexpanne1;
				$valueexpanne2 = $this->input->post('valueexpanne2');
				$marque_implant2=$valueexpanne2;
			}
			else if($iCnt==3){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$marque_implant1=$valueexpanne1;
				$valueexpanne2 = $this->input->post('valueexpanne2');
				$marque_implant2=$valueexpanne2;
				$valueexpanne3 = $this->input->post('valueexpanne3');
				$marque_implant3=$valueexpanne3;
			}
			$data = array(
				'id_clinique'=> $id_clinique, 
				/*'logo' => $_FILES["logo"]['name'],*/
				'nom_clinique'=>$this->input->post('nom_clinique'),
				'id_ville'=>$this->input->post('ville'),
				'id_pays'=>$this->input->post('pays'),
				'adresse'=>$this->input->post('adresse'),
				'code_postal'=>$this->input->post('code_postal'),
				/*'photo1' => $_FILES["photo1"]['name'],
				'photo2' => $_FILES["photo2"]['name'],
				'photo3' => $_FILES["photo3"]['name'],
				'photo4' => $_FILES["photo4"]['name'],
				'photo5' => $_FILES["photo5"]['name'],
               			'photo6' => $_FILES["photo6"]['name'],
				'photo7' => $_FILES["photo7"]['name'],
				'photo8' => $_FILES["photo8"]['name'],
				'photo9' => $_FILES["photo9"]['name'],
				'photo10' => $_FILES["photo10"]['name'],*/
				'prothese'=>$this->input->post('prothese'),
				'scanner'=>$this->input->post('scanner'),
				'origine_materiaux1'=>$this->input->post('origine_materiaux1'),
				'origine_materiaux2'=>$this->input->post('origine_materiaux2'),
				'origine_materiaux3'=>$this->input->post('origine_materiaux3'),
				'fauteuil'=>$this->input->post('fauteuil'),
				'surface'=>$this->input->post('surface'),
				'marque_implant1'=>$marque_implant1,
				'marque_implant2'=>$marque_implant2,     
				'marque_implant3'=>$marque_implant3,
				'isInfoUpdate'=>$isInfoUpdate
			);
			$this->MDn->updateC1($data);
			$this->load->library('upload');  
			//$id_clinique = $this->input->post('id_clinique');
                        for($i=1; $i<=10; $i++){
				$filename = 'photo'.$i;
				if (!empty($_FILES[$filename]['name']))
				{
				    // Specify configuration for File          
				    $config['upload_path'] = 'upload/';
				    $config['allowed_types'] = 'gif|jpg|png';
				    $config['max_size'] = '200000';
				    $config['max_width']  = '800';
				    $config['max_height']  = '600';  
				    // Initialize config for File 
				    $this->upload->initialize($config);
				    // Upload file 
				    if ($this->upload->do_upload($filename))
				    {
				        $data = $this->upload->data();
				        $url = $config['upload_path']."".$_FILES[$filename]['name'];
				    }
				    else
				    {
				        echo $this->upload->display_errors();
				    }
				}
	       	}			
			$data['result']= $this->MDn->getInfoClinicId($id_clinique);
			echo "<script>alert('Enregistré')</script>";
			$data['pays']= $this->MDn->getAllPays();
			$data['ville']= $this->MDn->getAllVille(1);
			$data['origine_materiaux3']= $this->MDn->getAllPays();
			$data['origine_materiaux2']= $this->MDn->getAllPays();
			$data['origine_materiaux1']= $this->MDn->getAllPays();
			$this->load->vars($data);
			$this->load->view('pagedn/header');
            		$this->load->view('pagedn/updateC1affiche');
			$this->load->view('pagedn/footer');
		}
		else {
			/*$data['result']= $this->MDn->getInfoClinicId($id_clinique);*/
                        $data['result']= $this->mod_clinic->getAllClinicInfo($id_clinique);
			/*$data['pays']= $this->MDn->getAllPays();*/
                        $data['pays']= $this->mod_clinic->getAllPays();
                        /*$data['ville']=$this->mod_clinic->getVilleFromPays($id_pays);*/
			/*$data['ville']= $this->MDn->getAllVille(1);*/
                        $data['origine_materiaux3']= $this->mod_clinic->getAllPays();
			$data['origine_materiaux2']= $this->mod_clinic->getAllPays();
			$data['origine_materiaux1']= $this->mod_clinic->getAllPays();
			/*$data['origine_materiaux3']= $this->MDn->getAllPays();
			$data['origine_materiaux2']= $this->MDn->getAllPays();
			$data['origine_materiaux1']= $this->MDn->getAllPays();*/
			/*$this->load->view('pagedn/header');
			$this->load->view('pagedn/updateC1', $data);
			$this->load->view('pagedn/footer');*/
                        $this->load->view('clinic/vue_editer_clinique',$data);
		}	
	}
        /*popup pour éditer l'espace clinique step la clinique*/
        function editer_laclinique(){
            $lg = $this->uri->segment(1,0);
            $_SESSION['lg']=$lg;
            $id_cli = $_SESSION['idid'];
            $this->lang->load('header',$lg);
            $this->lang->load('lien',$lg);
            $this->lang->load('container',$lg);
            $this->lang->load('footer',$lg);
            $data['result']= $this->mod_clinic->getAllClinicInfo($id_cli);
            $data['pays']= $this->mod_clinic->getAllPays();
            $data['origine_materiaux3']= $this->mod_clinic->getAllPays();
            $data['origine_materiaux2']= $this->mod_clinic->getAllPays();
            $data['origine_materiaux1']= $this->mod_clinic->getAllPays();
            $test =1;
            $this->load->view('clinic/vue_editer_laclinique',$data);
        }
        /*mise à jour step la cclinique*/
        function update_laclinique(){
            
        }
           
}